#include<iostream>
#include<assert.h>
#include<vector>
using namespace std;

namespace hj
{
  template<class T>
    class vector{
      public:
        typedef T* iterator;
        typedef const T* const_iterator;
        iterator begin()
        {
          return _start;
        }
        iterator end()
        {
          return _finish;
        }
        const_iterator begin()const
        {
          return _start;
        }
        const_iterator end()const
        {
          return _finish;
        }

        //构造
        vector()
          :_start(nullptr),
          _finish(nullptr),
          _en_of_storage(nullptr)
        {}
        //析构
        ~vector()
        {
          delete[] _start;
          _start = _finish = _en_of_storage;
        }
        //迭代器区间构造
        template <class InputIterator>
              vector(InputIterator first, InputIterator last)
                :_start(nullptr),
                _finish(nullptr),
                _en_of_storage(nullptr)
              {
                
                while(first != last)
                {
                  push_back(*first);
                  ++first;
                }
              }
       void swap(vector<T>& v)
        {
          std::swap(v._start,_start);
          std::swap(v._finish,_finish);
          std::swap(v._en_of_storage,_en_of_storage);
        }
        //拷贝构造
        vector(const vector<T>& v)
          :_start(nullptr),
          _finish(nullptr),
          _en_of_storage(nullptr)
        {
          vector<T> tmp(v.begin(),v.end());
          swap(tmp);
        }
        //赋值重载
        vector<T>& operator=(vector<T> v)
        {
          swap(v);
          return *this;
        }
        size_t size()
        {
          return _finish - _start;
        }
        size_t capacity()
        {
          return _en_of_storage - _start;
        }
        void reserve(size_t n)
        {
          if(n > capacity())
          {
            size_t len = size();
            T* tmp = new T[n];
            if(_start)//判断是否为空
            {

              for(size_t i = 0;i < len; i++)//拷贝数据
              {
                tmp[i] = _start[i];
              }
              delete[] _start;
            }
            _start = tmp;
            _finish = _start + len;
            _en_of_storage = _start + n;
          }
        }
        void resize(size_t n,T& val=T())
        {
          if(n>capacity())//扩容
          {
            reserve(n);
          }
          if(n>size())
          {
            while(_finish < _start + n)
            {
              *_finish = val;
              ++_finish;
            }
          }
          else
          {
            _finish = _start + n;
          }
        }
        void push_back(const T& val)
        {
          insert(_finish,val);
        }
        void pop_back()
        {
          erase(_finish-1);
        }
        iterator insert(iterator pos,const T& val)
        {
          assert(pos >=_start);
          assert(pos <=_finish);
          if(_finish == _en_of_storage)//判断扩容
          {
            size_t len = pos - _start;
            reserve(capacity()==0?4:capacity()*2);
            pos = _start + len;//更新扩容后pos的位置
          }
          iterator end = _finish-1;
          while(end >= pos)
          {
            *(end+1) = *(end);//用end = end-1在pos为_start时会越界
            end--;
          }
          *pos = val;
          _finish++;
          return pos;
        }

        iterator erase(iterator pos)
        {
          assert(pos >= _start);
          assert(pos < _finish);
          iterator begin = pos+1;
          while(begin < _finish)
          {
            *(begin-1) = *begin;
            begin++;
          }
          _finish--;
          return pos;
        }
        T& operator[](size_t sz)
        {
          assert(sz < size());
          return *(_start + sz);
        }
        const T& operator[](size_t sz)const
        {
          assert(sz < size());
          return *(_start + sz);
        }

        T& front()
        {
          assert(size()>0);
          return *_start;
        }
        T& back()
        {
          assert(size()>0);
          return *(_finish-1);
        }
      private:
        iterator _start;
        iterator _finish;
        iterator _en_of_storage;
    };
  void test_vector1()
  {
    vector<int> v1;//构造
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);
    v1.push_back(5);
    vector<int> v2(v1);//拷贝构造
    for(auto ch : v1)//迭代器
    {
      cout << ch << " ";
    }
    cout << endl;
    for(auto ch : v2)
    {
      cout << ch << " ";
    }
    cout << endl;
    for(size_t i=0;i<v2.size();i++)//[]重载
    {
      cout << v1[i] << " ";
    }
    cout << endl;
    vector<int> v3;
    v3=v1;//赋值重载

    for(auto ch : v3)
    {
      cout << ch << " ";
    }
    cout << endl;

    v3.erase(v3.begin());//删除
    v3.erase(v3.end()-1);
    for(auto ch : v3)
    {
      cout << ch << " ";
    }
    cout << endl;

    cout<< "size->"<<v1.size()<<endl;
    cout<< "capacity->"<<v1.capacity()<<endl;


  }
}
